using System;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Collections;

namespace ELS.Configuration
{
	#region LocalFileConfigurationProvider
	/// <summary>
	/// Provides file based persistence for configuration settings.
	/// </summary>
	public class LocalFileConfigurationProvider : ConfigurationProvider
	{
		#region variables
		private ConfigurationCollection _configurations = null;
        private string _path;
		#endregion variables

		#region constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="ELS.Configuration.LocalFileConfigurationProvider"/> class.
		/// </summary>
		/// <param name="path"> the file path for configuration data. </param>
		public LocalFileConfigurationProvider(string path)
		{
            _path = path;
            _configurations = new ConfigurationCollection();


            const string APPPATH = "$ApplicationPath";
            //if ( path.StartsWith(APPPATH) )
            //{
            //    string directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //    path = directory + path.Replace(APPPATH,"");
            //}

            path = @"E:\Title\Raman\033008\UserInterface\XMLFile1.xml";
			if ( File.Exists( path ) )
			{
				XmlDocument doc = new XmlDocument();
				doc.Load( path );
				
				XmlNamespaceManager nsmgr = new XmlNamespaceManager( doc.NameTable );
				nsmgr.AddNamespace( Constants.NAMESPACE_PREFIX, Constants.NAMESPACE_URI );

				string xpath = "//ns:configurationSettings/ns:configurations/ns:configuration";
				
				XmlNodeList configNodeList = doc.DocumentElement.SelectNodes(xpath , nsmgr);
				if ( configNodeList != null )
				{
					Configuration configuration = null;
					foreach( XmlNode configNode in configNodeList )
					{
						configuration = new Configuration( configNode.OuterXml );

                        if ( configuration != null && ! _configurations.Contains( configuration.Name ))
							_configurations[ configuration.Name  ] = configuration;
					}

				}
			}
		}

		#endregion constructors

		#region overrides
		/// <summary>
		/// Inherited from <see cref="ELS.Configuration.ProviderBase"/>.
		/// </summary>
		public override string Description
		{
			get { return "A class to provides file persistence for configuration settings."; }
		}

		/// <summary>
		/// Inherited from <see cref="ELS.Configuration.ProviderBase"/>.
		/// </summary>
		public override string Name
		{
			get	{ return "ELS.Configuration.LocalFileConfigurationProvider"; }
		}

		/// <summary>
		/// Inherited from <see cref="ELS.Configuration.ConfigurationProvider"/>.
		/// </summary>
		public override ConfigurationCollection Configurations
		{
			get	{ return _configurations; }
		}

        /// <summary>
        /// 
        /// </summary>
        public override void Save()
        {
            XmlDocument doc = new XmlDocument();
		    doc.LoadXml(_path);

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace(Constants.NAMESPACE_PREFIX, Constants.NAMESPACE_URI);

            string xpath = "//ns:configurationSettings/ns:configurations";
            XmlNode configurations = doc.SelectSingleNode(xpath);
            configurations.RemoveAll();
            foreach (Configuration config in _configurations)
            {
                XmlNode node = doc.CreateNode(XmlNodeType.Element,"configuration",null);
                node.InnerXml = config.GetXml();
                configurations.AppendChild(node);
            }
            doc.Save(_path);
        }
		#endregion overrides
    }
	#endregion LocalFileConfigurationProvider
}
